package com.school.service.Impl;

import com.school.common.Constants;
import com.school.common.TeaShopCategoryLevelEnum;
import com.school.controller.vo.IndexCategoryVO;
import com.school.controller.vo.SecondLevelCategoryVO;
import com.school.controller.vo.ThirdLevelCategoryVO;
import com.school.entity.GoodsCategory;
import com.school.mapper.GoodsCategoryMapper;
import com.school.service.GoodsCategoryService;
import com.school.util.BeanUtil;
import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service

public class GoodsCategoryServiceImpl implements GoodsCategoryService {
    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;


    @Override
    public PageResult getCategorysPage(PageQueryUtil pageQueryUtil) {
        List<GoodsCategory> list = goodsCategoryMapper.findGoodsCategoryList(pageQueryUtil);
        int totalCount = goodsCategoryMapper.getTotalGoodsCategoryCount(pageQueryUtil);
        return new PageResult(list, totalCount, pageQueryUtil.getLimit(), pageQueryUtil.getPage());
    }

    @Override
    public List<GoodsCategory> selectByLevelAndParentIdAndNumber(List<Long> parentsIds ,int categoryLevel) {
        return goodsCategoryMapper.selectByLevelAndParentIdAndNumber( parentsIds,categoryLevel,0);
    }

    @Override
    public GoodsCategory getGoodsCategoryById(Long categoryId) {
        return goodsCategoryMapper.selectById(categoryId);
    }

   @Override
public List<IndexCategoryVO> getCategoriesForIndex() {
    List<IndexCategoryVO> indexCategoryVOS = new ArrayList<>();

    // 非空：部分原因数据库的原因
    List<GoodsCategory> firstLevelCategories = goodsCategoryMapper.selectByLevelAndParentIdAndNumber(Collections.singletonList(0L), TeaShopCategoryLevelEnum.LEVEL_ONE.getLevel(), Constants.INDEX_CATEGORY_NUMBER);
    if (firstLevelCategories != null) {
        // 获取所有一级分类id
        ArrayList<Long> firstLevelCategoryIds = new ArrayList<>();
        for (GoodsCategory category : firstLevelCategories) {
            firstLevelCategoryIds.add(category.getCategoryId());
        }
        List<GoodsCategory> secondLevelCategories = goodsCategoryMapper.selectByLevelAndParentIdAndNumber(firstLevelCategoryIds, TeaShopCategoryLevelEnum.LEVEL_TWO.getLevel(), 0);
        if (!secondLevelCategories.isEmpty()) {
            ArrayList<Long> secondLevelCategoryIds = new ArrayList<>();
            for (GoodsCategory category : secondLevelCategories) {
                secondLevelCategoryIds.add(category.getCategoryId());
            }
            List<GoodsCategory> thirdLevelCategories = goodsCategoryMapper.selectByLevelAndParentIdAndNumber(secondLevelCategoryIds, TeaShopCategoryLevelEnum.LEVEL_THREE.getLevel(), 0);
            if (thirdLevelCategories != null) {
                // 根据parentId给ThirdLevelCategories分组
                Map<Long, List<GoodsCategory>> thirdLevelCategoryMap = thirdLevelCategories.stream().collect(Collectors.groupingBy(GoodsCategory::getParentId));
                List<SecondLevelCategoryVO> secondLevelCategoryVOS = new ArrayList<>();
                for (GoodsCategory secondLevelCategory : secondLevelCategories) {
                    SecondLevelCategoryVO secondLevelCategoryVO = new SecondLevelCategoryVO();
                    BeanUtil.copyProperties(secondLevelCategory, secondLevelCategoryVO);
                    if (thirdLevelCategoryMap.containsKey(secondLevelCategory.getCategoryId())) {
                        // 根据二级分类的id从thirdLevelCategoryMap中获取对应三级分类的list列表
                        List<GoodsCategory> tempGoodsCategories = thirdLevelCategoryMap.get(secondLevelCategory.getCategoryId());
                        secondLevelCategoryVO.setThirdLevelCategoryVOS(BeanUtil.copyList(tempGoodsCategories, ThirdLevelCategoryVO.class));
                        secondLevelCategoryVOS.add(secondLevelCategoryVO);
                    }
                }
                // 处理一级分类
                if (secondLevelCategoryVOS.size() > 0) {
                    Map<Long, List<SecondLevelCategoryVO>> secondLevelCategoryVOMap = secondLevelCategoryVOS.stream().collect(Collectors.groupingBy(SecondLevelCategoryVO::getParentId));
                    for (GoodsCategory firstCategory : firstLevelCategories) {
                        // 根据一级分类的id从map中获取此一级分类的所有二级分类
                        IndexCategoryVO indexCategoryVO = new IndexCategoryVO();
                        BeanUtil.copyProperties(firstCategory, indexCategoryVO);
                        // 如果一级分类下有数据则放入IndexCategoryVO中
                        if (secondLevelCategoryVOMap.containsKey(firstCategory.getCategoryId())) {
                            List<SecondLevelCategoryVO> tempSecondCategoryVOS = secondLevelCategoryVOMap.get(firstCategory.getCategoryId());
                            indexCategoryVO.setSecondLevelCategoryVOS(tempSecondCategoryVOS);
                            indexCategoryVOS.add(indexCategoryVO);
                        }
                    }
                }
            }
        }
    }
    return indexCategoryVOS;
}

}
